package com.usf.jprovider.schema.provider.file;

import java.io.File;
import java.io.FileReader;

import com.usf.jprovider.object.JObjectNode;
import com.usf.jprovider.object.env.Environment;
import com.usf.jprovider.object.type.JString;
import com.usf.jprovider.object.type.JType;

public final class TextFile extends AbstractFileSource{

	public TextFile() {
		super();
	}

	@Override
	protected String createUnformated(Environment env, JObjectNode node) throws Exception {		
		return (String)cacheValues(env, node);
	}
	@Override
	public boolean check(Environment env, JObjectNode node) throws Exception {
		return checkValue(env, node, new JString(node.getValue().toString())); 
	}
	@Override
	public boolean checkValue(Environment env, JObjectNode node, JType<?> value) throws Exception {		
		String values = (String)cacheValues(env, node);
		return value.getValue().equals(values);
	}

	@Override
	public Object values(Environment env) throws Exception {
		File file = new File(env.fullPath(path));
		FileReader reader = new FileReader(env.fullPath(path));
		char[] tab = new char[(int)file.length()];
		reader.read(tab);
		reader.close();
		return new String(tab);
	}
	@Override
	protected String cacheIdentifier() {
		return "file - " + path;
	}
}
